<template>
  <!-- 流程定义 -->
  <div>
    <el-row>
      <el-col style="margin-right: 10px" class="content-left">
        <el-row class="handleBox">
          <el-col :span="18">
            <template>
              <el-button type="normal" @click="process">流程进度</el-button>
              <el-button type="normal" @click="suspend">挂起</el-button>
              <el-button type="normal" @click="activate">激活</el-button>
              <el-button type="normal" @click="terminate">终止</el-button>
              <el-button type="normal" @click="reassignment()">改派</el-button>
            </template>
          </el-col>
          <el-col :span="6">
            <div class="handleIconBox">
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="search_porper">
                  <el-form
                    ref="searchForm"
                    :model="searchForm"
                    label-width="90px"
                    label-position="left"
                    size="mini"
                  >
                    <el-form-item label="流程名称：">
                      <el-input
                        v-model="searchForm.processDefName"
                        style="width: 180px"
                      />
                    </el-form-item>
                    <el-form-item label="发起人：">
                      <div class="treeBoxBig">
                        <div class="treeBox">
                          {{ searchForm.userName }}
                        </div>
                        <div class="treeBtnCS" @click="initiatorBtn" />
                      </div>
                    </el-form-item>
                    <el-form-item label="流程状态：">
                      <el-select
                        v-model="searchForm.processInstanceStatus"
                        placeholder="请选择"
                        style="width: 100%"
                      >
                        <el-option
                          v-for="item in statusList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-form>
                  <div style="text-align: center; padding: 10px">
                    <el-button
                      size="mini"
                      @click="(searchForm.page = 1), searchFunc()"
                      >查询</el-button
                    >
                    <el-button size="mini" @click="searchClear">重置</el-button>
                  </div>
                </div>
                <i slot="reference" class="iconfont icon-sousuo" title="搜索" />
              </el-popover>
              <el-popover
                placement="bottom"
                width="150"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="operation_checkbox">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox
                      v-for="item in textData"
                      :key="item.prop"
                      :label="item.label"
                      >{{ item.label }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <i
                  slot="reference"
                  class="iconfont icon-liebiaoshezhi"
                  title="列表设置"
                />
              </el-popover>
            </div>
          </el-col>
        </el-row>
        <el-table
          ref="multipleTable"
          :data="tableData"
          @row-click="authorizeFor"
        >
          <el-table-column width="55" type="selection"  />
          <el-table-column
            v-if="columnState[0]"
            prop="processDefName"
            label="流程名称"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnState[1]"
            prop="processInstanceUserName"
            label="发起人"
            :formatter="(row) => row.processInstanceUserName || '-'"
          />
          <el-table-column
            v-if="columnState[2]"
            prop="startedTm"
            label="流程发起时间"
            sortable
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div v-if="scope.row.startedTm">
                {{ timeFormat(scope.row.startedTm) }}
              </div>
              <div v-else>-</div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnState[3]"
            prop="taskName"
            label="当前任务"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnState[4]"
            prop="taskApprover"
            label="未操作人"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnState[5]"
            prop="status"
            label="流程状态"
            :filter-method="(val, row) => row.status === val"
            :filters="[
              { text: '运行中', value: '1' },
              { text: '已结束', value: '2' },
              { text: '已终止', value: '3' },
              { text: '错误', value: '4' },
              { text: '挂起', value: '5' },
            ]"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.status == 1">
                <i class="startState" /><span>运行中</span>
              </div>
              <div v-if="scope.row.status == 2">
                <i class="startState" /><span style="color: red">已结束</span>
              </div>
              <div v-if="scope.row.status == 3">
                <i class="startState" /><span style="color: red">已终止</span>
              </div>
              <div v-if="scope.row.status == 4">
                <i class="unStartState" /><span style="color: red">错误</span>
              </div>
              <div v-if="scope.row.status == 5">
                <i class="unStartState" /><span style="color: red">挂起</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnState[6]"
            prop="finishedTm"
            label="流程结束时间"
            sortable
            show-overflow-tooltip
            :formatter="(row) => row.finishedTm || '-'"
          />
        </el-table>
        <pagination
          :current-page="searchForm.page"
          :page-size="searchForm.size"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @refresh="searchFunc"
        />
      </el-col>
    </el-row>
    <!-- 审批弹框 -->
    <el-dialog
      v-dialogDrag
      title="流程进度"
      :visible.sync="taskVisible"
      width="50%"
    >
      <commonTab
        ref="commonTab"
        :businessKey="businessKey"
        :processInstanceId="processInstanceId"
        :save-titles="saveTitles"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="taskVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <!--改派弹框 -->
    <el-dialog
      v-dialogDrag
      title="选择"
      :visible.sync="reassignmentVisible"
      width="50%"
    >
      <reassignmentTable
        ref="reassignmentTable"
        @sPrincipalUser="sPrincipalUser"
        @approvered="approvered"
        @reassignClose="reassignClose"
      />
    </el-dialog>
    <!--改派弹框 -->
  </div>
</template>
<script>
import reassignmentTable from "@/views/workFlow/components/reassignmentTable";
import commonTab from "@/views/workFlow/components/commonTab";
import { workflow } from "@/api/index";
import moment from "moment";
export default {
  components: {
    reassignmentTable,
    commonTab,
  },
  data() {
    return {
      businessKey: "",
      processInstanceId: "",
      row: [],
      statusList: [
        { value: "1", label: "运行中" },
        { value: "2", label: "已结束" },
        { value: "3", label: "已终止" },
        { value: "4", label: "错误" },
        { value: "5", label: "挂起" },
      ],
      reassignmentVisible: false,
      saveTitles: {
        list: [
          {
            title: "任务审批",
            show: false,
          },
          {
            title: "审批历史",
            show: true,
          },
          {
            title: "流程进度图",
            show: true,
          },
          {
            title: "业务表单",
            show: false,
          },
        ],
        activeName: "second",
      },
      taskVisible: false,
      searchForm: {
        processDefCode: "",
        processDefName: "",
        taskDefName: "",
        userName: "",
        processInstanceStatus: "",
        page: 1,
        size: 15,
      },
      total: 0,
      checkList: [],
      // 列隐藏
      textData: [
        { label: "流程名称" },
        { label: "发起人" },
        { label: "流程发起时间" },
        { label: "当前任务" },
        { label: "未操作人" },
        { label: "流程状态" },
        { label: "流程结束时间" },
      ],
      // 获取table的数据
      tableData: [],
    };
  },
  computed: {
    columnState() {
      const arr = [];
      const arr1 = this.textData;
      const arr2 = this.checkList;
      for (let i = 0; i < arr1.length; i++) {
        let flg = false;
        for (let j = 0; j < arr2.length; j++) {
          if (arr1[i].label === arr2[j]) {
            flg = true;
          }
        }
        arr.push(flg);
      }
      return arr;
    },
  },
  watch: {},
  created() {
    this.checkListInit();
    this.searchFunc();
  },
  mounted() {},
  methods: {
    reassignment() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      if (selection[0].status !== "1") {
        return this.$message.warning("请选择运行中的数据");
      }
      this.row = selection;
      this.reassignmentVisible = true;
      setTimeout(() => {
        this.$refs.reassignmentTable.init(selection[0]);
        this.$refs.reassignmentTable.getOrgList();
      }, 30);
    },
    // 点击发起人
    initiatorBtn() {
      this.reassignmentVisible = true;
      setTimeout(() => {
        this.$refs.reassignmentTable.init({ id: "0" });
        this.$refs.reassignmentTable.getOrgList();
      }, 30);
    },
    reassignClose() {
      this.reassignmentVisible = false;
    },
    // 选择的发起人
    approvered(val) {
      // 改派
      const data = {
        taskId: this.row[0].taskId,
        userName: val.userName,
      };
      workflow.taskDelegate(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("成功");
          this.reassignmentVisible = false;
        } else {
          this.$message.error(res.message || "失败");
        }
      });
    },
    sPrincipalUser(val) {
      this.searchForm.userName = val.userName;
      this.reassignmentVisible = false;
    },
    searchClear() {
      for (const key in this.searchForm) {
        this.searchForm[key] = "";
      }
      this.searchForm.page = 1;
      this.searchForm.size = 15;
    },
    checkListInit() {
      this.checkList = this.textData.map((v) => {
        return v.label;
      });
    },
    searchFunc() {
      const data = this.searchForm;
      workflow.queryForPageProcessInstanceHistory(data).then((res) => {
        this.tableData = res.datas.rows;
        this.total = res.datas.total;
      });
    },
    // 流程进度
    process() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      this.businessKey = selection[0].businessKey;
      this.processInstanceId = selection[0].processInstanceId;
      this.row = selection;
      this.taskVisible = true;
      setTimeout(() => {
        this.$refs.commonTab.init();
      }, 30);
    },
    // 挂起
    suspend() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      if (selection[0].status !== "1") {
        return this.$message.warning("请选择运行中的数据");
      }
      const data = {
        processInstanceId: selection[0].processInstanceId,
      };
      workflow.suspend(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("成功");
          this.searchFunc();
        } else {
          this.$message.error(res.message || "失败");
        }
      });
    },
    // 激活
    activate() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      if (selection[0].status !== "5") {
        return this.$message.warning("请选择挂起的数据");
      }
      const data = {
        processInstanceId: selection[0].processInstanceId,
      };
      workflow.activate(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("成功");
          this.searchFunc();
        } else {
          this.$message.error(res.message || "失败");
        }
      });
    },
    // 终止
    terminate() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      const data = {
        processInstanceId: selection[0].processInstanceId,
      };
      workflow.terminate(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("成功");
          this.searchFunc();
        } else {
          this.$message.error(res.message || "失败");
        }
      });
    },
    authorizeFor(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);

      this.userName = row.roleName;
    },
    timeFormat(val) {
      if (val) return moment(val).format("YYYY-MM-DD HH:mm:ss");
      else return "";
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchForm.size = val;
      this.searchFunc();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.searchForm.page = val;
      this.searchFunc();
    },
  },
};
</script>

<style lang="scss" scoped>
.treeBoxBig {
  width: 180px;
  height: 32px;
  border: 1px solid #dce0e6;
  border-radius: 4px;
  color: white;
  .treeBox {
    width: 140px;
    height: 32px;
    padding-left: 10px;
    line-height: 32px;
  }
  .treeBtnCS {
    background: url("~@/assets/images/popWindow_arrow.gif");
    background-repeat: no-repeat;
    height: 32px;
    width: 32px;
    position: absolute;
    left: 156px;
    top: 8px;
    cursor: pointer;
  }
}
</style>
